﻿// Launcher.cpp : 定义应用程序的入口点。
//

#include "framework.h"
#include "Launcher.h"

#define MAX_LOADSTRING 100

// 全局变量:
HINSTANCE hInst;                                // 当前实例
CHAR szTitle[MAX_LOADSTRING];                  // 标题栏文本
CHAR szWindowClass[MAX_LOADSTRING];            // 主窗口类名

// 此代码模块中包含的函数的前向声明:
ATOM                MyRegisterClass(HINSTANCE hInstance);
BOOL                InitInstance(HINSTANCE, int);
LRESULT CALLBACK    WndProc(HWND, UINT, WPARAM, LPARAM);
INT_PTR CALLBACK    About(HWND, UINT, WPARAM, LPARAM);
HINSTANCE _hInstance;

int APIENTRY wWinMain(_In_ HINSTANCE hInstance,
                     _In_opt_ HINSTANCE hPrevInstance,
                     _In_ LPWSTR    lpCmdLine,
                     _In_ int       nCmdShow)
{
    UNREFERENCED_PARAMETER(hPrevInstance);
    UNREFERENCED_PARAMETER(lpCmdLine);

    // TODO: 在此处放置代码。
    bw = GetSystemMetrics(SM_CXSCREEN) / 6, bh = GetSystemMetrics(SM_CYSCREEN) / 6;
    RECT rect = { 0,0,bw * 2 , bh };

    AdjustWindowRect(&rect, WS_OVERLAPPEDWINDOW, false);

   
    // 初始化全局字符串
    LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
    LoadString(hInstance, IDC_LAUNCHER, szWindowClass, MAX_LOADSTRING);
    MyRegisterClass(hInstance);

    // 执行应用程序初始化:
    HWND hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
        CW_USEDEFAULT, 0, rect.right - rect.left, rect.bottom - rect.top, nullptr, nullptr, hInstance, nullptr);

    if (!hWnd)
    {
        return FALSE;
    }

    ShowWindow(hWnd, nCmdShow);
    UpdateWindow(hWnd);

    _hInstance = hInstance;
    HACCEL hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_LAUNCHER));

    MSG msg;

    // 主消息循环:
    while (GetMessage(&msg, nullptr, 0, 0))
    {
        if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
        {
            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }
    }

    return (int) msg.wParam;
}



//
//  函数: MyRegisterClass()
//
//  目标: 注册窗口类。
//
ATOM MyRegisterClass(HINSTANCE hInstance)
{
    WNDCLASSEX wcex;

    wcex.cbSize = sizeof(WNDCLASSEX);

    wcex.style          = CS_HREDRAW | CS_VREDRAW;
    wcex.lpfnWndProc    = WndProc;
    wcex.cbClsExtra     = 0;
    wcex.cbWndExtra     = 0;
    wcex.hInstance      = hInstance;
    wcex.hIcon          = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_LAUNCHER));
    wcex.hCursor        = LoadCursor(nullptr, IDC_ARROW);
    wcex.hbrBackground  = (HBRUSH)(COLOR_WINDOW+1);
    wcex.lpszMenuName   = NULL;
    wcex.lpszClassName  = szWindowClass;
    wcex.hIconSm        = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));

    return RegisterClassEx(&wcex);
}

//
//   函数: InitInstance(HINSTANCE, int)
//
//   目标: 保存实例句柄并创建主窗口
//
//   注释:
//
//        在此函数中，我们在全局变量中保存实例句柄并
//        创建和显示主程序窗口。
//
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
   hInst = hInstance; // 将实例句柄存储在全局变量中

  

   return TRUE;
}

//
//  函数: WndProc(HWND, UINT, WPARAM, LPARAM)
//
//  目标: 处理主窗口的消息。
//
//  WM_COMMAND  - 处理应用程序菜单
//  WM_PAINT    - 绘制主窗口
//  WM_DESTROY  - 发送退出消息并返回
//
//
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    switch (message)
    {

    case WM_CREATE: {
        
        Btn_help = CreateWindowEx(WS_EX_CLIENTEDGE, "BUTTON", "How to use it 帮助", WS_VISIBLE | WS_CHILD | BS_PUSHBUTTON, 0, 0 , bw,bh, hWnd, (HMENU)0, _hInstance, NULL);
        Btn_start = CreateWindowEx(WS_EX_CLIENTEDGE, "BUTTON", "Start 启动保护", WS_VISIBLE | WS_CHILD | BS_PUSHBUTTON, bw, 0, bw, bh, hWnd, (HMENU)0, _hInstance, NULL);
        break;
    }
    case WM_COMMAND:
        {
        if ((HWND)lParam == Btn_help) {
            MessageBox(hWnd, "REMEMBER:original passcode is 123456.\n切记:Protecter的初始解锁密码为123456", "Notice", MB_ICONQUESTION);
            MessageBox(hWnd, "ATTENTION:ScreenProtecter may be deleted by your anti-virus software because of the HOOK technique,please trust it before running it\n注意:ScreenProtecter也许会被杀毒软件误杀，请添加信任", "Notice", MB_ICONEXCLAMATION);
            ShellExecute(hWnd, "open", "HowToUse.txt", "", "", SW_SHOWNORMAL);
        }
        else if ((HWND)lParam == Btn_start) {
            if (MessageBox(hWnd, "我已知悉使用说明，可以开始运行软件\nI have learnt the instrustion,and is able to run the software", "Caution", MB_YESNO | MB_ICONINFORMATION) == IDYES)
                if ((DWORD)ShellExecute(hWnd, "open", "ScreenProtecter.exe", "", "", SW_SHOWNORMAL) <= 32)
                    MessageBox(hWnd, "错误!无法打开ScreenProtecter.exe\nFATAL:Can't open ScreenProtecter.exe", "Error", MB_ICONERROR);

        }
        }
        break;
    case WM_PAINT:
        {
            PAINTSTRUCT ps;
            HDC hdc = BeginPaint(hWnd, &ps);
            // TODO: 在此处添加使用 hdc 的任何绘图代码...
            EndPaint(hWnd, &ps);
        }
        break;
    case WM_DESTROY:
        PostQuitMessage(0);
        break;
    default:
        return DefWindowProc(hWnd, message, wParam, lParam);
    }
    return 0;
}

